#!/bin/sh
#
########## SET THE CUSTOM VARIABLES ##########
#
# 设置自定义变量
# 当该程序运行结束，此处设置的变量自动取消
#
# USB_MOUNT_POINT: The mount point of the USB flash drive
# 设置自定义变量USB_MOUNT_POINT为U盘挂载点
USB_MOUNT_POINT=$(find / -name 'script_bootloader' | awk -F / '{print "/"$2"/"$3"/"$4}')
#
# SBL_SYS_BIN: The directory which contains all the executable files related to SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER
# 设置自定义变量SBL_SYS_BIN为SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER系统可执行文件目录
SBL_SYS_BIN=${USB_MOUNT_POINT}/script_bootloader/bin
#
# SBL_SYS_USR: The directory which contains all the external software resources (add-ons) related to SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER
# 设置自定义变量SBL_SYS_USR为SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER系统外部软件资源目录，即插件目录
SBL_SYS_USR=${USB_MOUNT_POINT}/script_bootloader/usr
#
# NAME OF THE SERVICE
# 服务名称
SERVICE_NAME="ADDONS"
#
# FUNCTION OF THE SERVICE
# 服务功能
SERVICE_FUNCTION="PREPARATION"
#
########## END ##########
#
#
########## ADDONS PREPARATION ##########
#
tar xzvf ./SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER-ADDONS*.tar.gz -C ${USB_MOUNT_POINT}/
if [ $? != 0 ]
then
    echo -e "\033[1;41;37m***** FAILED *****\033[0m"
    exit 1
fi
chmod -R 777 ${USB_MOUNT_POINT}/SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER-ADDONS*/
if [ $? != 0 ]
then
    echo -e "\033[1;41;37m***** FAILED *****\033[0m"
    exit 2
fi
rm -f ${USB_MOUNT_POINT}/SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER-ADDONS*/ChangeLog.md
if [ $? != 0 ]
then
    echo -e "\033[1;41;37m***** FAILED *****\033[0m"
    exit 3
fi
rm -f ${USB_MOUNT_POINT}/SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER-ADDONS*/LICENSE
if [ $? != 0 ]
then
    echo -e "\033[1;41;37m***** FAILED *****\033[0m"
    exit 4
fi
rm -f ${USB_MOUNT_POINT}/SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER-ADDONS*/README.md
if [ $? != 0 ]
then
    echo -e "\033[1;41;37m***** FAILED *****\033[0m"
    exit 5
fi
rm -f ${USB_MOUNT_POINT}/SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER-ADDONS*/addons_prepare
if [ $? != 0 ]
then
    echo -e "\033[1;41;37m***** FAILED *****\033[0m"
    exit 6
fi
mv ${USB_MOUNT_POINT}/SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER-ADDONS*/addons_install ${SBL_SYS_BIN}/
if [ $? != 0 ]
then
    echo -e "\033[1;41;37m***** FAILED *****\033[0m"
    exit 7
fi
mv ${USB_MOUNT_POINT}/SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER-ADDONS*/* ${SBL_SYS_USR}/
if [ $? != 0 ]
then
    echo -e "\033[1;41;37m***** FAILED *****\033[0m"
    exit 8
fi
rmdir ${USB_MOUNT_POINT}/SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER-ADDONS*/
if [ $? != 0 ]
then
    echo -e "\033[1;41;37m***** FAILED *****\033[0m"
    exit 9
fi
rm -f ${USB_MOUNT_POINT}/addons_prepare
if [ $? != 0 ]
then
    echo -e "\033[1;41;37m***** FAILED *****\033[0m"
    exit 10
fi
#
echo -e "\033[1;42;37m***** SUCCEEDED *****\033[0m"
#
########## END ##########